# 项目: PythonProject1
# 文件: test01
# 作者: Zheng Hao
# 日期: 2025/5/20 18:22
# coding:utf-8


from selenium import webdriver
import time,random

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support.select import Select


def get_driver():

    driver = webdriver.Edge()
    driver.implicitly_wait(5)   # 隐式等待5秒
    driver.maximize_window()    # 最大化窗口
    return driver

def input(element, value):
    element.click()     # 点击输入框
    element.clear()     # 清空原有内容
    element.send_keys(value)    # 输入值

def select_random_first(element):
    options_count = len(Select(element).options)    # 获取下拉选项总数
    random_index = random.randint(1, options_count-1)       # 随机索引（跳过第0项）
    Select(element).select_by_index(random_index)       # 按索引选择

if __name__ == '__main__':

    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/login/')
    uname = driver.find_element('name', 'userName')
    input(uname,'YD0006')       # 输入用户名
    upass = driver.find_element('name', 'userPass')
    input(upass, 'maxiaoliuYD0006')     # 输入密码
    driver.find_element('css selector', '#form-login > div > div > div.modal-footer > button').click()      # 点击登录
    time.sleep(3)


    driver.get('http://localhost:8080/YunDuanBoss/borrow')
    driver.find_element('css selector', '#content > div.row.con-margin.con-body-con > div:nth-child(1) > div > button:nth-child(1)').click()        # 点击"新增登记"按钮

    barcode = driver.find_element('css selector', '#addBor-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > input:nth-child(2)')
    input(barcode, '98865532101')        # 输入资产条码

    assets_type = driver.find_element('css selector',
                                      '#addBor-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > select:nth-child(2)')
    select_random_first(assets_type)  # 随机选择设备类型

    purchase_employee = driver.find_element('css selector',
                                            '#addBor-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(2)')
    input(purchase_employee, '马小六')
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.DOWN).perform()  # 按下箭头选择
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.ENTER).perform()  # 回车确认
    time.sleep(2)

    driver.find_element('css selector',
                        '#addAssBtn').click()
    time.sleep(3)
















